package com.ywicc.commlibs.socket;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/03/16
 * desc   : Wifi 信息获取工具类封装
 * version: 1.0
 */
public class WifiUtils {

    private final DhcpInfo dhcpInfo;
    private final WifiInfo wifiInfo;

    public WifiUtils(Context context) {
        // 获取 WifiManager 对象
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        dhcpInfo = wifiManager.getDhcpInfo();
        wifiInfo = wifiManager.getConnectionInfo();
    }

    /**
     * 获取本机 IP
     *
     * @return 返回本机 IP
     */
    @SuppressWarnings("deprecation")
    public String getSelfIP() {
        return Formatter.formatIpAddress(wifiInfo.getIpAddress());
    }

    /**
     * 获取服务器端 IP (网关 IP)
     *
     * @return 返回服务器端 IP (网关 IP)
     */
    @SuppressWarnings("deprecation")
    public String getServerIP() {
        return Formatter.formatIpAddress(dhcpInfo.gateway);
    }

}
